from PyQt5.QtWidgets import QCheckBox, QLabel, QPushButton, QWidget, QVBoxLayout, QFileDialog, QLineEdit, QHBoxLayout, QMessageBox
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import QThread, pyqtSignal
from utils.tile_manger_tools import Tile_Tools
from utils.dingTalk_message_reboot import message


class Zip_To_Webp_Widget(QWidget):
    def __init__(self):
        super(QWidget, self).__init__()
        self.init_ui()

    def init_ui(self):
        self.resize(400, 250)
        self.setFixedSize(self.width(), self.height())
        self.setWindowTitle("ZIP包转换为webp瓦片")
        self.setWindowIcon(QIcon("./assets/window_icon.png"))

        vbox = QVBoxLayout()
        input_line = QHBoxLayout()
        output_line = QHBoxLayout()
        check_box_line = QHBoxLayout()
        button_line = QHBoxLayout()
        vbox.addLayout(input_line)
        vbox.addLayout(output_line)
        vbox.addLayout(check_box_line)
        vbox.addLayout(button_line)

        self.setLayout(vbox)

        # 输入
        input_label = QLabel("输入：")
        self.input_edit_line = QLineEdit()
        input_dialog_botton = QPushButton("...")
        input_line.addWidget(input_label)
        input_line.addWidget(self.input_edit_line)
        input_line.addWidget(input_dialog_botton)
        input_dialog_botton.clicked.connect(self.click_input_button)

        # 输出
        output_label = QLabel("输出：")
        self.output_edit_line = QLineEdit()
        output_dialog_botton = QPushButton("...")
        output_line.addWidget(output_label)
        output_line.addWidget(self.output_edit_line)
        output_line.addWidget(output_dialog_botton)
        output_dialog_botton.clicked.connect(self.click_output_button)

        # 单选框
        self.check_box = QCheckBox("处理完成向钉钉发送提示消息！")
        check_box_line.addWidget(self.check_box)
        # 提交按钮
        self.submit_button = QPushButton("提交处理")
        button_line.addWidget(self.submit_button)
        self.submit_button.clicked.connect(self.click_submit_button)

    def click_input_button(self):
        input_dir_path = QFileDialog.getExistingDirectory()
        if input_dir_path:
            self.input_edit_line.setText(input_dir_path)

    def click_output_button(self):
        output_dir_path = QFileDialog.getExistingDirectory()
        if output_dir_path:
            self.output_edit_line.setText(output_dir_path)

    def click_submit_button(self):
        if self.input_edit_line.text() and self.output_edit_line.text():
            self.submit_button.setEnabled(False)
            self.check_box.setEnabled(False)
            self.hide()
            self.thread = Complex_thread(
                self.input_edit_line.text(), self.output_edit_line.text())
            self.thread.start()
            self.thread.exit()
            self.thread.singnal.connect(self.tip)
        else:
            QMessageBox.warning(self, "错误", "请补全输入输出文件夹路径", QMessageBox.Yes)

    def tip(self):
        self.submit_button.setEnabled(True)
        self.check_box.setEnabled(True)
        if self.check_box.isChecked():
            secret = 'SEC22cf798f3e28ab4d822add8af6b5319f41d28b7d650330a52842d3f087dd8f09'
            webhook = 'https://oapi.dingtalk.com/robot/send?access_token=fe51c3ebbacee696c300210610eba7ec5f20c20c0199e70b3c1a1fc19d292c49'
            message("切片数据整理完成！",webhook,secret)
        QMessageBox.information(self, "信息", "完成处理！", QMessageBox.Yes)



class Complex_thread(QThread):
    singnal = pyqtSignal()

    def __init__(self, input_edit_line, output_edit_line):
        super().__init__()
        self.input_edit_line = input_edit_line
        self.output_edit_line = output_edit_line

    def run(self):
        tool = Tile_Tools()
        tool.zip_to_webp_function(self.input_edit_line, self.output_edit_line)
        self.singnal.emit()
